#include "endpoint.h"

#include <QDebug>
#include <QFile>
#include <QMessageBox>
#include <QStandardItemModel>

#include "digitalkeyboard.h"
#include "ui_endpoint.h"

EndPoint::EndPoint(QWidget* parent) : QDialog(parent), ui(new Ui::EndPoint) {
  ui->setupUi(this);
  readConfig();
  // connect( this, SIGNAL( accepted() ), this, SLOT( accept() ) );
  // connect( this, SIGNAL( rejected() ), this, SLOT( reject() ) );
  QString button_style(
      "QPushButton{border-image: url(:/pic/src/button/key.png);color: white;}"
      "QPushButton:hover{border-image: url(:/pic/src/button/key_hover.png);color: white;}"
      "QPushButton:pressed{border-image: url(:/pic/src/button/key_pressed.png);color: black;}");
  ui->QDialogButtonBox_ok->setStyleSheet(button_style);
  ui->pushButton_cancle->setStyleSheet(button_style);
  ui->comboBox_2->setStyleSheet(button_style);
  ui->tabWidget->setCurrentIndex(0);
  ui->radioButton_end_id->setChecked(true);
  ui->lineEdit_input_num->setValidator(new QIntValidator(-65535, 65535, this));
}

EndPoint::~EndPoint() { delete ui; }

void EndPoint::readConfig() {
  const QString fileName = "./ini/endpoint.csv";
  QFile routeFile(fileName);
  QStringList csvList;
  csvList.clear();
  if (!routeFile.exists()) {
    QString tips = QString("打开%1文件失败").arg(fileName);
    qCritical() << tips;
    return;
  }
  if (!routeFile.open(QIODevice::ReadOnly)) { return; }
  QTextStream stream(&routeFile);
  while (!stream.atEnd()) { csvList.push_back(stream.readLine().replace("\"", "")); }  //保存到List当中
  routeFile.close();
  QStringList xbtmplist;
  QStringList tmplist;
  QList<QPair<int, SlamRoutePlanning::RouteType>> endpttmplist;
  gxlist.clear();
  xblist.clear();
  endptlist.clear();
  selectgx = 0;
  selectpt = 0;
  QString str, s;
  for (int i = 0; i < csvList.size(); i++) {
    str = csvList.at(i);
    if (i % 3 == 0) {
      tmplist = str.split(",");
      gxlist.append(tmplist.at(0));
    } else if (i % 3 == 1) {
      tmplist = str.split(",");
      xbtmplist.clear();
      foreach (s, tmplist) {
        if (!s.isEmpty()) xbtmplist.append(s);
      }
      xblist.append(xbtmplist);
    } else if (i % 3 == 2) {
      tmplist = str.split(",");
      endpttmplist.clear();
      foreach (s, tmplist) {
        int id;
        SlamRoutePlanning::RouteType type;
        SlamRoutePlanning::getIdAndRouteType(s, id, type);

        if (id <= 0) { continue; }
        endpttmplist.append({id, type});
      }
      endptlist.append(endpttmplist);
    }
  }

  QStandardItemModel* model = new QStandardItemModel();
  model->clear();
  for (int i = 0; i < gxlist.size(); i++) { model->setItem(i, 0, new QStandardItem(gxlist.at(i))); }
  ui->comboBox->setModel(model);
  // ui->comboBox->clear();
  // ui->comboBox->addItems( gxlist );
}

void EndPoint::on_comboBox_currentIndexChanged(int index) {
  QStandardItemModel* model = new QStandardItemModel();
  QList<QString> tmp;
  tmp = xblist.at(index);
  selectgx = index;
  model->clear();
  for (int i = 0; i < tmp.size(); i++) { model->setItem(i, 0, new QStandardItem(tmp.at(i))); }
  ui->comboBox_2->setModel(model);
}

void EndPoint::on_comboBox_2_currentIndexChanged(int index) {
  selectpt = endptlist.at(selectgx).at(index).first;
  route_type = endptlist.at(selectgx).at(index).second;
}

int EndPoint::getSelectPoint() const { return selectpt; }

int EndPoint::getSelectRoute() const { return selectroute; }

bool EndPoint::isSetRoute() { return is_set_route; }

// bool EndPoint::isForward() { return forward; }

SlamRoutePlanning::RouteType EndPoint::routeType() { return route_type; }

void EndPoint::on_QDialogButtonBox_ok_clicked() {
  int index = ui->tabWidget->currentIndex();
  if (index == 0) {  //工序页面
    is_set_route = false;
  } else {                                      //非工序页面
    if (ui->radioButton_end_id->isChecked()) {  //输入终点id
      is_set_route = false;
      //      selectpt = abs(ui->lineEdit_input_num->text().toInt());
      //      forward = ui->lineEdit_input_num->text().toInt() >= 0;

      int id;
      SlamRoutePlanning::RouteType type;
      SlamRoutePlanning::getIdAndRouteType(ui->lineEdit_input_num->text(), id, type);

      if (id <= 0) { return; }

      selectpt = id;
      route_type = type;

    } else if (ui->radioButton_route_id->isChecked()) {  //输入路线id
      is_set_route = true;
      selectroute = ui->lineEdit_input_num->text().toInt();
    }
  }
  accept();
}

void EndPoint::on_pushButton_cancle_clicked() { reject(); }

void EndPoint::on_pushButton_open_digikeybord_clicked() {
  Digitalkeyboard digi(this);
  digi.setWindowTitle(tr("数字输入"));
  digi.exec();
  if (digi.isOk()) { ui->lineEdit_input_num->setText(digi.getTheNumber()); }
}
